package com.example.demo.controller;

import com.example.demo.entity.Chapter;
import com.example.demo.service.ChapterService;
import com.example.demo.utils.R;
import com.example.demo.utils.ReSet;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@CrossOrigin
@RestController
public class ChapterController {
    @Resource
    private ChapterService chapterService;

    /**
     * 增加章节
     * */
    @PostMapping("insertchapter")
    public R insertchapter(String chapter_name, Integer chapter_type,Integer chapter_node){
        int i = chapterService.insertchapter(chapter_name,chapter_type,chapter_node);
        if (i > 0){
            return ReSet.Ok(i);
        }
        return ReSet.Err();
    }

    @GetMapping("selchapter1")
    public R selchapter1(){
        List<Chapter> list = chapterService.selchapter1();
        if (list != null){
            return ReSet.Ok(list);
        }
        return ReSet.Err();
    }
    /**
     * 章节查询
     * */
    @GetMapping("selchapter")
    public R selchapter(){
        List<Chapter> list = chapterService.selchapter();
        if (list != null){
            return ReSet.Ok(list);
        }
        return ReSet.Err();
    }
    /**
     * 单章节所属章节查询
     * */
    @PostMapping("selonecha")
    public R selonecha(Integer chapter_id){
        Chapter chapters = chapterService.selnodes(chapter_id);
        Chapter chapter = chapterService.selonecha(chapter_id);
        if(chapters.getChapterNode() != 0){
            if (chapter != null){
                return ReSet.Ok(chapter.getChapterName());
            }
        }else {
            return ReSet.Ok("信息系统项目管理系统");
        }

        return ReSet.Err();
    }
    /**
     * 章节名查询
     * */
    @PostMapping("selcnm")
    public R selcnm(Integer chapter_id){
        Chapter chapter = chapterService.selcnm(chapter_id);
        if (chapter != null){
            return ReSet.Ok(chapter);
        }else {
            return ReSet.Err();
        }
    }
    /**
     * 父级章节查询
     * */
    @PostMapping("selfath")
    public R selfath(){
        List<Chapter> chapters = chapterService.selfath();
        if (chapters != null){
            return ReSet.Ok(chapters);
        }
        return ReSet.Err();
    }
    /**
     * 更改章节
     * */
    @Async
    @PostMapping("updtchapter")
    public R updtchapter(String chapter_name,Integer chapter_type, Integer chapter_id){
        int i = chapterService.updtchapter(chapter_name,chapter_type,chapter_id);
        if (i > 0){
            return ReSet.Ok(i);
        }
        return ReSet.Err();
    }
    /**
     * 删除章节(父级目录+子目录)
     * */
    @PostMapping("delchapter")
    public R delchapter(Integer chapter_id){
        int i = chapterService.delchapter(chapter_id);
        if (i > 0){
            return ReSet.Ok(i);
        }
        return ReSet.Err();
    }
    /**
     * 删除子目录
     * */
    @PostMapping("delone")
    public R delone(Integer chapter_id) {
        int i = chapterService.delone(chapter_id);
        if (i > 0){
            return ReSet.Ok(i);
        }
        return ReSet.Err();
    }
    /**
     * 更改章节状态
     * */
    @PostMapping("updtchatype")
    public R updtchatype(Integer chapter_type, Integer chapter_id){
        int i = chapterService.updtchatype(chapter_type,chapter_id);
        if (i > 0){
            return ReSet.Ok(i);
        }
        return ReSet.Err();
    }
}
